﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Diagnostics;
using System.ComponentModel;

namespace Vwlab
{
    public class Update
    {
        Uri updateUrl = new Uri("http://update.vwlab.ru/winbird/");

        string[] info;
        Error err = new Error();

        public bool getUpdate(string currentVersion)
        {
            try
            {
                using (WebClient webClient = new WebClient())
                {
                    string content = webClient.DownloadString(updateUrl).ToString();

                    info = content.Split('|');

                    Version v1 = new Version(currentVersion);
                    Version v2 = new Version(info[0]);

                    if (v2 > v1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public void download()
        {
            try
            {
                using (WebClient webClient = new WebClient())
                {
                    webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(CompletedUpdate);
                    webClient.DownloadFileAsync(new Uri(updateUrl + info[2]), "winbird-update.zip");
                    
                }
            }
            catch (Exception)
            {
                err.setError("Не удалось скачать файл с обновлениями");
            }
        }

        private void CompletedUpdate(object sender, AsyncCompletedEventArgs e)
        {
            Process.Start("update.exe", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
        }
    }
}
